#include "WPH_server.h"
#include "viewPump.h"
#include "sys_log.h"

//查看水泵信息5
void viewInfo(char*buf,int sock)
{
    int num = 0;
    char type[30] = {0};
    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(type ,node->valuestring);
    }
    node = cJSON_GetObjectItem(json,"num");
    if(node == NULL)
    {
        LOG_E("can't find num\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        num = node->valueint;
    }
    
    cJSON_Delete(json);	

    char sql[1024] = {0};

    MYSQL *mysql = NULL;
  	MYSQL *rs = NULL;
    mysql = mysql_init(NULL);

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
 	}

    LOG_D("type = %s\n",type);

    if(strcmp(type,"view") == 0)
    {
        if(num == 1)
        {
            strcpy(sql,"select * from wph1");
        }
        else if(num == 2)
        {
            strcpy(sql,"select * from wph2");
        }
        else if(num == 3)
        {
            strcpy(sql,"select * from wph3");
        }
    }
    
 	LOG_D("sql:%s\n",sql);
 	int ret = 0;
	ret = mysql_query(mysql,sql);
	if(ret != 0)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
	}
    memset(&type,0,strlen(type));
	MYSQL_RES *res = NULL;
	MYSQL_ROW row = NULL;

    struct pump pump;
    memset(&pump,0,sizeof(pump));

	res = mysql_store_result(mysql);//把查询的数据取出，放到mysql中
    row = mysql_fetch_row(res);
    
    while(row != NULL)
    {  
        strcpy(type,"begin");
        strcpy(pump.time,row[0]);
        pump.flow = atoi(row[1]);
        pump.temperature = atoi(row[2]);
        pump.liquidlevel = atoi(row[3]);
        pump.stress = atof(row[4]);
        strcpy(pump.valve,row[5]);

        json = cJSON_CreateObject();
        
        //添加多个"键-值”对
        cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
        cJSON_AddItemToObject(json,"time",cJSON_CreateString(pump.time));
        cJSON_AddItemToObject(json,"flow",cJSON_CreateNumber(pump.flow));
        cJSON_AddItemToObject(json,"temperature",cJSON_CreateNumber(pump.temperature));
        cJSON_AddItemToObject(json,"liquidlevel",cJSON_CreateNumber(pump.liquidlevel));
        cJSON_AddItemToObject(json,"stress",cJSON_CreateNumber(pump.stress));
        cJSON_AddItemToObject(json,"valve",cJSON_CreateString(pump.valve));
       
       int len = sizeof(pump);
       printf("%d\n",len);

        //将json对象转成json格式字符串
        char *p = cJSON_Print(json);
        printf("%s\n",p);
        //发送
        ret = send(sock,p,strlen(p),0);
        if(ret == -1)
        {
            LOG_E("send\n");
            exit(-1);
        
        }   
        memset(p,0,strlen(p));
        row = mysql_fetch_row(res);
        sleep(1);
    }

    //给发送结束标志
    strcpy(type,"over");
    strcpy(pump.time,"0");
    pump.flow = 0;
    pump.temperature = 0;
    pump.liquidlevel = 0;
    pump.stress = 0;
    strcpy(pump.valve,"0");

    json = cJSON_CreateObject();
        
    //添加多个"键-值”对
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
    cJSON_AddItemToObject(json,"time",cJSON_CreateString(pump.time));
    cJSON_AddItemToObject(json,"flow",cJSON_CreateNumber(pump.flow));
    cJSON_AddItemToObject(json,"temperature",cJSON_CreateNumber(pump.temperature));
    cJSON_AddItemToObject(json,"liquidlevel",cJSON_CreateNumber(pump.liquidlevel));
    cJSON_AddItemToObject(json,"stress",cJSON_CreateNumber(pump.stress));
    cJSON_AddItemToObject(json,"valve",cJSON_CreateString(pump.valve));

    char *p = cJSON_Print(json);
    
    ret = send(sock,p,strlen(p),0);
    if(ret == -1)
    {
        LOG_E("send\n");
        exit(-1);
    } 

    printf("发送完成！\n");
    mysql_free_result(res);
    mysql_close(mysql);

    cJSON_Delete(json);
}

